package com.example.fragload;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.fragload.fragments.SecondFragment;
import com.example.fragload.fragments.FirstFragment;
import com.example.fragload.fragments.ThirdFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HideShowActivity extends AppCompatActivity {

    @BindView(R.id.nav_view)
    BottomNavigationView navView;
    @BindView(R.id.nav_host_fragment)
    FrameLayout contentFragment;
    Fragment[] fragments = new Fragment[3];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        ButterKnife.bind(this);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
//        ft.addToBackStack(null);
        fragments[0] = new FirstFragment();
        ft.add(R.id.nav_host_fragment, fragments[0]);
        ft.commitAllowingStateLoss();

        navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.navigation_user:
                        switchContent(getCurrentFragment(), fragments[0]);
                        return true;
                    case R.id.navigation_room:
                        if (fragments[1] == null) {
                            fragments[1] = new SecondFragment();
                            addContent(getCurrentFragment(), fragments[1]);
                        } else {
                            switchContent(getCurrentFragment(), fragments[1]);
                        }
                        return true;
                    case R.id.navigation_setting:
                        if (fragments[2] == null) {
                            fragments[2] = new ThirdFragment();
                            addContent(getCurrentFragment(), fragments[2]);
                        } else {
                            switchContent(getCurrentFragment(), fragments[2]);
                        }
                        return true;
                }
                return false;
            }
        });
    }

    public Fragment getCurrentFragment() {
        Fragment currentFragment = null;
        FragmentManager fragmentManager = getSupportFragmentManager();
        List<Fragment> fragments = fragmentManager.getFragments();
        if (null != fragments) {
            for (Fragment tmp : fragments) {
                if (tmp != null && tmp.isVisible()) {
                    currentFragment = tmp;
                }
            }
        }
        return currentFragment;
    }


    public void switchContent(Fragment from, Fragment to) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(from).show(to).commitAllowingStateLoss(); // 隐藏当前的fragment，显示下一个
    }

    public void addContent(Fragment from, Fragment to) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(from).add(R.id.nav_host_fragment, to).commitAllowingStateLoss(); // 隐藏当前的fragment，显示下一个
    }
}
